package com.lt.setimage;


import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by lt on 2016/10/11.
 * <p>
 * android 6.0  动态权限
 */

public class MySystemPermissions {
    //判断是否大于或者==23；
    private static final boolean VERSION = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    // 相机权限组
    public static final int CAMERAS_REQUEST = 0x10;
    private static String[] Cameras = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    //存储权限
    public static final int STORAGES_REQUEST = 0x11;
    private static String[] Storages = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    //联系人权限组
    public static final int CONTACTS_REQUEST = 0x12;
    private static String[] Contacts = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.GET_ACCOUNTS};

    //拨打电话
    public static final int CALL_PHONE = 0x13;
    private static String[] CALLS = {Manifest.permission.CALL_PHONE};

    //调用相机权限
    public static boolean setCameraPermissions(final Activity activity, String title, String message) {
        if (VERSION && ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            if (!ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    Manifest.permission.CAMERA)) {

                DialogHelper.twoButtonDialog(activity, title, message, "确定", "取消", new DialogHelper.DialogCallback() {
                    @Override
                    public void determine() {
                        ActivityCompat.requestPermissions(activity,
                                Cameras,
                                CAMERAS_REQUEST);
                    }

                    @Override
                    public void cancel() {

                    }
                });
                return false;
            }
            ActivityCompat.requestPermissions(activity,
                    Cameras,
                    CAMERAS_REQUEST);
            return false;
        }
        return true;
    }

    //调用存储权限
    public static boolean setStoragePermissions(final Activity activity, String title, String message) {
        if (VERSION && ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                DialogHelper.twoButtonDialog(activity, title, message, "确定", "取消", new DialogHelper.DialogCallback() {
                    @Override
                    public void determine() {
                        ActivityCompat.requestPermissions(activity,
                                Storages,
                                STORAGES_REQUEST);
                    }

                    @Override
                    public void cancel() {

                    }
                });

                return false;
            }
            ActivityCompat.requestPermissions(activity,
                    Storages,
                    STORAGES_REQUEST);
            return false;
        }
        return true;
    }

    //拨打电话
    public static boolean setCALLPermissions(final Activity activity, String title, String message) {
        if (VERSION && ActivityCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    Manifest.permission.CALL_PHONE)) {

                DialogHelper.twoButtonDialog(activity, title, message, "确定", "取消", new DialogHelper.DialogCallback() {
                    @Override
                    public void determine() {
                        ActivityCompat.requestPermissions(activity,
                                CALLS,
                                CALL_PHONE);
                    }

                    @Override
                    public void cancel() {

                    }
                });


                return false;
            }
            ActivityCompat.requestPermissions(activity,
                    CALLS,
                    CALL_PHONE);
            return false;
        }
        return true;
    }

    //调用联系人权限
    public static boolean setContactsPermissions(final Activity activity, String title, String message) {
        if (VERSION && ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    Manifest.permission.READ_CONTACTS)) {

                DialogHelper.twoButtonDialog(activity, title, message, "确定", "取消", new DialogHelper.DialogCallback() {
                    @Override
                    public void determine() {
                        ActivityCompat.requestPermissions(activity,
                                Contacts,
                                CONTACTS_REQUEST);
                    }

                    @Override
                    public void cancel() {

                    }
                });


                return false;
            }
            ActivityCompat.requestPermissions(activity,
                    Contacts,
                    CONTACTS_REQUEST);
            return false;
        }
        return true;
    }


}
